import traceback
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime

import pythoncom
import win32com.client


executor = ThreadPoolExecutor(max_workers=1)
def create_event(subject, start, end, busy=2, location=""):
    """
    创建outlook日程
    :param subject:
    :param start:
    :param end:
    :param busy:
    :param location:
    :return:
    """
    try:
        pythoncom.CoInitialize()

        if isinstance(start, datetime):
            start_str = start.strftime('%m-%d-%Y %H:%M:%S')
        else:
            start_str = start

        if isinstance(end, datetime):
            end_str = end.strftime('%m-%d-%Y %H:%M:%S')
        else:
            end_str = end
        print(start_str,end_str)
        outlook = win32com.client.Dispatch("Outlook.Application")
        appt = outlook.CreateItem(1)
        appt.Start = start_str
        appt.Subject = subject
        appt.End = end_str
        appt.BusyStatus = busy
        appt.Location = location
        appt.MeetingStatus = 0
        appt.Save()
        appt.Send()
    except Exception as e:
        print(f"创建事件时出错: {str(e)}")
        traceback.print_exc()
    finally:
        pythoncom.CoUninitialize()


def schedule_event_sync(subject, start, end, busy, location):

    executor.submit(create_event, subject, start, end, busy, location)

if __name__ == '__main__':
    # create_event(subject='',start='2022-06-01 15:00:00',end='2022-06-01 18:00:00',location='')
    schedule_event_sync(subject='',start='2025-03-14 18:00:00',end='2025-03-14 19:00:00',location='',busy=0)